const app = new getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    uid: 0,
    promoter_detail: {},
    member_info: {},
    count: 0,
    goods_list: [],
    mygoodslist:0,
    default_img: {},
    category_id: '',
    brand_id: '',
    obyzd: '',
    st: '',
    page: 1,
    page_size: 10,
    type: 'selected'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    app.getMemberId(this, 'loadDefaultImg');
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  loadDefaultImg: function () {
    app.defaultImg(this, true);
  },

  loadInfo: function() {
    let that = this;
    app.sendRequest({
      url: 'System.Distribution.promoterDetailByUid',
      data: {
        uid: that.data.uid
      },
      success: function(res) {
        that.setData({
          promoter_detail: res.data
        })
      }
    })

    app.sendRequest({
      url: 'System.Member.memberInfo',
      data: {},
      success: function(res) {
        console.log(res);

        let member_info = res.data;
        member_info.user_info.user_headimg = app.IMG(member_info.user_info.user_headimg);
        that.setData({
          member_info: member_info
        })
      }
    })

    app.sendRequest({
      url: 'System.Distribution.distributionGoodsCount',
      data: {},
      success: function(res) {
        that.setData({
          count: res.data
        })
      }
    })

    app.sendRequest({
      url: 'System.Distribution.distributionGoodsList',
      data: {
        category_id: '',
        brand_id: '',
        obyzd: '',
        st: '',
        page: 1,
        page_size: 10,
        uid: that.data.uid,
        type: 'selected'
      },
      success: function(res) {
        let goods_list = app.checkEmpty(res.data.data, []);
        let mygoodslist = 0;
        for (let index in goods_list) {
          goods_list[index].pic_cover_small = app.IMG(goods_list[index].pic_cover_small);
          mygoodslist++;
        }
        that.setData({
          goods_list: goods_list,
          mygoodslist: mygoodslist
        })
      }
    })
  },

  /**
   * 头像加载失败
   */
  errorHeadImg: function (e) {
    let that = this;
    let member_info = that.data.member_info;
    let default_img = that.data.default_img;
    let img = member_info.user_info.user_headimg;
    let parm = {};
    let parm_key = "member_info.user_info.user_headimg";

    if (default_img.is_use == 1) {
      default_img = default_img.value.default_headimg;
      if (img.indexOf(default_img) == -1) {
        parm[parm_key] = default_img;
        that.setData(parm);
      }
    }
  },

  /**
   * 商品图加载失败
   */
  errorGoodsImg: function() {
    let that = this;
    let default_img = that.data.default_img;
    let goods_list = that.data.goods_list;
    let index = e.currentTarget.dataset.index;
    let img = goods_list[index].pic_cover_small;
    let parm = {};
    let parm_key = "goods_list[" + index + "].pic_cover_small";

    if (default_img.is_use == 1) {
      let default_goods_img = default_img.value.default_goods_img;
      if (img.indexOf(default_goods_img) == -1) {
        let parm_key = "goods_list[" + index + "].pic_cover_small";

        parm[parm_key] = default_goods_img;
        that.setData(parm);
      }
    }
  }
})